import QtQuick 2.0
import QtQuick.Controls 2.3
import "storage.js" as Storage

Item {
    id:root
    width: 850
    height: 580
    property real minSize:  Math.min(width/850, height/580)
    property real textSize:  40*minSize
    property color majorColor: "#44fdfd"

    onFocusChanged:{
        if(!focus) return
        refurbishA()
        refurbishB()
    }

    function readRegSingalLim(handle, sqlName){
        handle.valueText = Storage.readRegSingalLimit(sqlName)
    }
    function writeRegSingalLim(handle, sqlName){
        Storage.writeRegSingalLimit(handle.valueText, sqlName)
    }
    function readRegScope(handle, sqlName){
        handle.tipText = Storage.readRegScope(sqlName)
    }

    function refurbishA()
    {
        readRegSingalLim(ta1_a, "A套自动PID TA1")
        readRegSingalLim(ta2_a, "A套自动PID TA2")
        readRegSingalLim(ta3_a, "A套自动PID TA3")
        readRegSingalLim(ta4_a, "A套自动PID TA4")
        readRegSingalLim(kavr_a, "A套自动PID Kavr")
    }
    function refurbishB()
    {
        readRegSingalLim(ta1_b, "B套自动PID TA1")
        readRegSingalLim(ta2_b, "B套自动PID TA2")
        readRegSingalLim(ta3_b, "B套自动PID TA3")
        readRegSingalLim(ta4_b, "B套自动PID TA4")
        readRegSingalLim(kavr_b, "B套自动PID Kavr")
    }

    Timer{
        interval: 2000
        repeat: false
        triggeredOnStart: false
        running: true
        onTriggered: {
            readRegScope(ta1_a, "A套自动PID TA1")
            readRegScope(ta2_a, "A套自动PID TA2")
            readRegScope(ta3_a, "A套自动PID TA3")
            readRegScope(ta4_a, "A套自动PID TA4")
            readRegScope(kavr_a, "A套自动PID Kavr")
            readRegScope(ta1_b, "B套自动PID TA1")
            readRegScope(ta2_b, "B套自动PID TA2")
            readRegScope(ta3_b, "B套自动PID TA3")
            readRegScope(ta4_b, "B套自动PID TA4")
            readRegScope(kavr_b, "B套自动PID Kavr")
        }
    }

    SwipeView
    {
        id: sv_this
        width:parent.width
        height: parent.height*0.85
        clip: true
        Component.onCompleted:{contentItem.highlightMoveDuration = 0}       //将移动时间设为0

        onCurrentIndexChanged: {
            if(sv_this.currentIndex===0) {button.checked=true;button2.checked=false}
            if(sv_this.currentIndex===1) {button2.checked=true;button.checked=false}
        }

        Item{
            Text {
                y: 50*root.minSize
                color: majorColor
                text: qsTr("A通道AVR参数")
                anchors.horizontalCenter: parent.horizontalCenter
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                font.pixelSize: 40*root.minSize
            }

            Image {
                anchors.horizontalCenter: parent.horizontalCenter
                y: 150*root.minSize
                width: sourceSize.width*root.minSize
                height: sourceSize.height*root.minSize
                source: "../picture/AVR_NEW.png"
                fillMode: Image.PreserveAspectFit

                EXC_Normal_DoubleTextSytle2 {
                    id: kavr_a
                    x: 240*root.minSize
                    y: 55*root.minSize
                    textSize: 22*root.minSize
                    funcText: "Kavr:"
                }
                EXC_Normal_DoubleTextSytle2 {
                    id: ta1_a
                    x: 365*root.minSize
                    y: 55*root.minSize
                    textSize: 22*root.minSize
                    funcText: "TA1:"
                }
                EXC_Normal_DoubleTextSytle2 {
                    id: ta2_a
                    x: 365*root.minSize
                    y: 140*root.minSize
                    textSize: 22*root.minSize
                    funcText: "TA2:"
                }
                EXC_Normal_DoubleTextSytle2 {
                    id: ta3_a
                    x: 490*root.minSize
                    y: 55*root.minSize
                    textSize: 22*root.minSize
                    funcText: "TA3:"
                }

                EXC_Normal_DoubleTextSytle2 {
                    id: ta4_a
                    x: 490*root.minSize
                    y: 140*root.minSize
                    textSize: 22*root.minSize
                    funcText: "TA4:"
                }
            }

            EXC_Normal_BtnUncheckSecret_highStyle {
                id: btn_ASave
                nameFontpixelSize: 25*root.minSize
                x: 670*root.minSize
                y: 410*root.minSize
                width: 140*root.minSize
                height: 50*root.minSize
                nameText: qsTr("保存参数")
                onClicked: {
                    writeRegSingalLim(ta1_a, "A套自动PID TA1")
                    writeRegSingalLim(ta2_a, "A套自动PID TA2")
                    writeRegSingalLim(ta3_a, "A套自动PID TA3")
                    writeRegSingalLim(ta4_a, "A套自动PID TA4")
                    writeRegSingalLim(kavr_a, "A套自动PID Kavr")
                    timerA_ass.start()
                }

                Timer{
                    id:timerA_ass
                    interval: 700
                    repeat: true
                    triggeredOnStart: false
                    property int thisCounter: 0
                    onTriggered: {
                        refurbishA()
                        thisCounter++
                        if(thisCounter>3) {
                            thisCounter=0
                            stop()
                        }
                    }
                }
            }

            onFocusChanged:{
                if(!focus) return
                refurbishA()
            }
        }

        Item{
            Text {
                y: 50*root.minSize
                color: majorColor
                text: qsTr("B通道AVR参数")
                anchors.horizontalCenter: parent.horizontalCenter
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                font.pixelSize: 40*root.minSize
            }

            Image {
                anchors.horizontalCenter: parent.horizontalCenter
                y: 150*root.minSize
                width: sourceSize.width*root.minSize
                height: sourceSize.height*root.minSize
                source: "../picture/AVR_NEW.png"
                fillMode: Image.PreserveAspectFit

                EXC_Normal_DoubleTextSytle2 {
                    id: kavr_b
                    x: 240*root.minSize
                    y: 55*root.minSize
                    textSize: 22*root.minSize
                    funcText: "Kavr:"
                }
                EXC_Normal_DoubleTextSytle2 {
                    id: ta1_b
                    x: 365*root.minSize
                    y: 55*root.minSize
                    textSize: 22*root.minSize
                    funcText: "TA1:"
                }
                EXC_Normal_DoubleTextSytle2 {
                    id: ta2_b
                    x: 365*root.minSize
                    y: 140*root.minSize
                    textSize: 22*root.minSize
                    funcText: "TA2:"
                }
                EXC_Normal_DoubleTextSytle2 {
                    id: ta3_b
                    x: 490*root.minSize
                    y: 55*root.minSize
                    textSize: 22*root.minSize
                    funcText: "TA3:"
                }

                EXC_Normal_DoubleTextSytle2 {
                    id: ta4_b
                    x: 490*root.minSize
                    y: 140*root.minSize
                    textSize: 22*root.minSize
                    funcText: "TA4:"
                }
            }

            EXC_Normal_BtnUncheckSecret_highStyle {
                id: btn_BSave
                nameFontpixelSize: 25*root.minSize
                x: 670*root.minSize
                y: 410*root.minSize
                width: 140*root.minSize
                height: 50*root.minSize
                nameText: qsTr("保存参数")
                onClicked: {
                    writeRegSingalLim(ta1_b, "B套自动PID TA1")
                    writeRegSingalLim(ta2_b, "B套自动PID TA2")
                    writeRegSingalLim(ta3_b, "B套自动PID TA3")
                    writeRegSingalLim(ta4_b, "B套自动PID TA4")
                    writeRegSingalLim(kavr_b, "B套自动PID Kavr")
                    timerB_ass.start()
                }

                Timer{
                    id:timerB_ass
                    interval: 700
                    repeat: true
                    triggeredOnStart: false
                    property int thisCounter: 0
                    onTriggered: {
                        if(thisCounter>0)
                            refurbishB()
                        thisCounter++
                        if(thisCounter>3) {
                            thisCounter=0
                            stop()
                        }
                    }
                }
            }

            onFocusChanged:{
                if(!focus) return
                refurbishB()
            }
        }
    }

    EXC_Normal_BtnSytle {
        id: button
        nameText: qsTr("A通道")
        width: parent.width*0.15
        height: parent.height*0.09
        checked: true
        anchors.left: parent.left
        anchors.leftMargin: parent.width*0.1
        anchors.topMargin: parent.height/32
        anchors.top: sv_this.bottom
        onClicked: {
            if(sv_this.currentIndex===0) checked = true;
            else sv_this.currentIndex = 0
        }
    }

    EXC_Normal_BtnSytle {
        id: button2
        nameText: qsTr("B通道")
        width: parent.width*0.15
        height: parent.height*0.09
        anchors.left: parent.left
        anchors.leftMargin: parent.width*0.43
        anchors.topMargin: parent.height/32
        anchors.top: sv_this.bottom
        onClicked: {
            if(sv_this.currentIndex===1) checked = true;
            else sv_this.currentIndex = 1
        }
    }

    EXC_Normal_BtnUncheckStyle {
        id: button3
        nameText: qsTr("返回")
        nameFontpixelSize: 30*Math.min(root.width/850, root.height/580)
        width: parent.width*0.15
        height: parent.height*0.09
        anchors.left: parent.left
        anchors.leftMargin: parent.width*0.75
        anchors.topMargin: parent.height/32
        anchors.top: sv_this.bottom
        onClicked: {
            sw.currentIndex = 0
        }
    }
}
